package cn.edu.sdjzu.xg.eagle.util;


import com.alibaba.fastjson2.JSON;

public class Pagination {
    //返回LIMIT子句
    public String toLimitClause(int totalNum){
        //向前台提供元组总数（totalNum为0不是错误）
        this.totalNum = totalNum;
        //偏移量
        int offset = (this.pageNo - 1) * this.pageSize;
        //输出LIMIT子句，注意关键字两端的空格
        return " LIMIT " + offset + ", " + this.pageSize;
    }

    public Pagination(int pageSize, int pageNo) {
        //防止错误赋值
        if(pageNo > 0) {
            this.pageNo = pageNo;
        }

        if(pageSize > 0) {
            this.pageSize = pageSize;
        }

    }
    //如果没有get方法，fastjson将无法读取对象属性
    public int getPageSize() {
        return pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getTotalNum() {
        return totalNum;
    }

    //每页的对象(元组)数
    private int pageSize = 5;
    //页号
    private int pageNo = 1;
    //对象(元组)总数
    private int totalNum = 1;

    @Override
    public String toString() {
        return "Pagination{" +
                "pageSize=" + pageSize +
                ", pageNo=" + pageNo +
                ", totalNum=" + totalNum +
                '}';
    }

    public static void main(String[] args) {

        int totalNum = 23;
        int maxPageNo = totalNum/10;
        Pagination pagination = new Pagination(10, 5);
        for(int i = 1; i <= maxPageNo; i++){
            System.out.println(i);
            System.out.println(pagination.toLimitClause(totalNum));
        }
        //创建JSON对象message，以便往前端响应信息

//        JSONObject message = new JSONObject();

        Object json = JSON.toJSON(pagination);
        System.out.println(json);
    }
}
